package com.mm.cloth;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.mm.cloth.model.Category;
import com.mm.cloth.model.Clothes;
import com.mm.cloth.model.Season;
import com.mm.cloth.utils.LoginCheckUtil;

import java.util.ArrayList;
import java.util.List;

public class MatchActivity extends AppCompatActivity {
    
    // UI组件
    private RecyclerView categoryList;
    private LinearLayout clothesListOverlay;
    private TextView clothesListTitle;
    private TextView closeBtn;
    private RecyclerView clothesList;
    private LinearLayout emptyClothesHint;
    private LinearLayout boardArea;
    private TextView boardTitle;
    private Button btnCancel;
    private Button btnSave;
    private Button btnOutfitList;
    private FrameLayout boardContent;
    private TextView emptyHint;
    private TextView debugInfo;
    private ConstraintLayout saveModal;
    private TextView modalTitle;
    private TextView modalClose;
    private EditText etOutfitName;
    private TextView tvInputCount;
    private RecyclerView previewItems;
    private Button btnModalCancel;
    private Button btnModalConfirm;
    private LinearLayout seasonList;
    
    // 数据
    private List<Category> categories = new ArrayList<>();
    private List<Clothes> allClothes = new ArrayList<>();
    private List<Clothes> filteredClothes = new ArrayList<>();
    private List<Clothes> boardItems = new ArrayList<>();
    private List<Season> seasons = new ArrayList<>();
    private int currentCategory = 0; // 全部分类的ID为0
    private String currentSeason = "all";
    private boolean showClothesList = false;
    private boolean isEditMode = false;
    private String editOutfitId = null;
    
    // 拖动相关
    private boolean isDragging = false;
    private float startX = 0;
    private float startY = 0;
    private int currentIndex = -1;
    
    // 适配器
    private CategoryAdapter categoryAdapter;
    private ClothesAdapter clothesAdapter;
    private PreviewAdapter previewAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match);
        
        // 初始化UI组件
        initViews();
        
        // 设置事件监听
        setupListeners();
        
        // 初始化数据
        initData();
        
        // 检查是否是编辑模式
        checkEditMode();
    }
    
    private void initViews() {
        categoryList = findViewById(R.id.categoryList);
        clothesListOverlay = findViewById(R.id.clothesListOverlay);
        clothesListTitle = findViewById(R.id.clothesListTitle);
        closeBtn = findViewById(R.id.closeBtn);
        clothesList = findViewById(R.id.clothesList);
        emptyClothesHint = findViewById(R.id.emptyClothesHint);
        boardArea = findViewById(R.id.boardArea);
        boardTitle = findViewById(R.id.boardTitle);
        btnCancel = findViewById(R.id.btnCancel);
        btnSave = findViewById(R.id.btnSave);
        btnOutfitList = findViewById(R.id.btnOutfitList);
        boardContent = findViewById(R.id.boardContent);
        emptyHint = findViewById(R.id.emptyHint);
        debugInfo = findViewById(R.id.debugInfo);
        saveModal = findViewById(R.id.saveModal);
        modalTitle = findViewById(R.id.modalTitle);
        modalClose = findViewById(R.id.modalClose);
        etOutfitName = findViewById(R.id.etOutfitName);
        tvInputCount = findViewById(R.id.tvInputCount);
        previewItems = findViewById(R.id.previewItems);
        btnModalCancel = findViewById(R.id.btnModalCancel);
        btnModalConfirm = findViewById(R.id.btnModalConfirm);
        seasonList = findViewById(R.id.seasonList);
        
        // 设置RecyclerView
        categoryList.setLayoutManager(new LinearLayoutManager(this));
        clothesList.setLayoutManager(new GridLayoutManager(this, 3));
        previewItems.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        
        // 初始化适配器
        categoryAdapter = new CategoryAdapter();
        clothesAdapter = new ClothesAdapter();
        previewAdapter = new PreviewAdapter();
        
        categoryList.setAdapter(categoryAdapter);
        clothesList.setAdapter(clothesAdapter);
        previewItems.setAdapter(previewAdapter);
    }
    
    private void setupListeners() {
        // 关闭衣物列表
        closeBtn.setOnClickListener(v -> closeClothesList());
        
        // 保存按钮
        btnSave.setOnClickListener(v -> saveOutfit());
        
        // 取消编辑
        btnCancel.setOnClickListener(v -> cancelEdit());
        
        // 搭配列表
        btnOutfitList.setOnClickListener(v -> showOutfitList());
        
        // 弹窗关闭
        modalClose.setOnClickListener(v -> closeSaveModal());
        
        // 弹窗取消
        btnModalCancel.setOnClickListener(v -> closeSaveModal());
        
        // 弹窗确认
        btnModalConfirm.setOnClickListener(v -> confirmSave());
        
        // 输入框监听
        etOutfitName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            
            @Override
            public void afterTextChanged(Editable s) {
                tvInputCount.setText(s.length() + "/20");
            }
        });
    }
    
    private void initData() {
        // 初始化季节数据
        initSeasons();
        
        // 加载分类数据
        loadCategories();
        
        // 加载衣物数据
        loadClothesByCategory(0); // 0表示全部分类
    }
    
    private void initSeasons() {
        seasons.add(new Season("all", "全部"));
        seasons.add(new Season("spring", "春季"));
        seasons.add(new Season("summer", "夏季"));
        seasons.add(new Season("autumn", "秋季"));
        seasons.add(new Season("winter", "冬季"));
        
        // 创建季节选择器
        createSeasonSelector();
    }
    
    private void createSeasonSelector() {
        seasonList.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(this);
        
        for (Season season : seasons) {
            TextView seasonView = (TextView) inflater.inflate(R.layout.item_season, seasonList, false);
            seasonView.setText(season.getName());
            seasonView.setSelected(season.getValue().equals(currentSeason));
            
            seasonView.setOnClickListener(v -> {
                // 更新选中状态
                for (int i = 0; i < seasonList.getChildCount(); i++) {
                    seasonList.getChildAt(i).setSelected(false);
                }
                v.setSelected(true);
                
                // 更新当前季节
                currentSeason = season.getValue();
                
                // 重新加载衣物
                loadClothesByCategory(currentCategory);
            });
            
            seasonList.addView(seasonView);
        }
    }
    
    private void checkEditMode() {
        // 检查是否是编辑模式
        Intent intent = getIntent();
        if (intent != null) {
            isEditMode = "edit".equals(intent.getStringExtra("mode"));
            editOutfitId = intent.getStringExtra("outfitId");
            
            if (isEditMode) {
                boardTitle.setText("编辑搭配");
                btnCancel.setVisibility(View.VISIBLE);
                
                if (editOutfitId != null) {
                    // 加载搭配数据
                    loadOutfitForEdit();
                }
            }
        }
    }
    
    private void loadCategories() {
        // 模拟加载分类数据
        categories.clear();
        
        // 添加"全部"分类
        categories.add(new Category(0, "全部", "wardrobe.png"));
        
        // 添加其他分类
        categories.add(new Category(1, "上衣", "topCloth.png"));
        categories.add(new Category(2, "下装", "下装.png"));
        categories.add(new Category(3, "连衣裙", "连衣裙.png"));
        categories.add(new Category(4, "外套", "外套.png"));
        categories.add(new Category(5, "鞋子", "shoes.png"));
        categories.add(new Category(6, "配饰", "decoration.png"));
        
        // 通知适配器数据已更新
        categoryAdapter.notifyDataSetChanged();
    }
    
    private void loadClothesByCategory(int categoryId) {
        // 更新当前分类
        currentCategory = categoryId;
        
        // 模拟加载衣物数据
        allClothes.clear();
        
        // 添加模拟数据
        for (int i = 1; i <= 20; i++) {
            Clothes clothes = new Clothes();
            clothes.setId(i);
            clothes.setName("衣物" + i);
            
            // 随机分配分类
            String category;
            int catId;
            switch (i % 6) {
                case 0: 
                    category = "上衣";
                    catId = 1;
                    break;
                case 1: 
                    category = "下装";
                    catId = 2;
                    break;
                case 2: 
                    category = "连衣裙";
                    catId = 3;
                    break;
                case 3: 
                    category = "外套";
                    catId = 4;
                    break;
                case 4: 
                    category = "鞋子";
                    catId = 5;
                    break;
                default: 
                    category = "配饰";
                    catId = 6;
            }
            clothes.setCategory(category);
            
            // 随机分配季节
            String season;
            switch (i % 4) {
                case 0: season = "spring"; break;
                case 1: season = "summer"; break;
                case 2: season = "autumn"; break;
                default: season = "winter";
            }
            clothes.setSeason(season);
            
            // 设置图片URL（实际应用中应该使用真实图片）
            clothes.setImageUrl("https://via.placeholder.com/150?text=Clothes" + i);
            
            allClothes.add(clothes);
        }
        
        // 过滤数据
        filterClothes();
        
        // 更新UI
        updateClothesListUI();
    }
    
    private void filterClothes() {
        filteredClothes.clear();
        
        for (Clothes clothes : allClothes) {
            // 分类筛选
            if (currentCategory != 0) {
                // 这里需要根据实际情况进行过滤
                String clothesCategory = clothes.getCategory();
                String expectedCategory = getCategoryName(currentCategory);
                if (!clothesCategory.equals(expectedCategory)) {
                    continue;
                }
            }
            
            // 季节筛选
            if (!"all".equals(currentSeason) && !clothes.getSeason().equals(currentSeason)) {
                continue;
            }
            
            filteredClothes.add(clothes);
        }
    }
    
    private void updateClothesListUI() {
        // 更新标题
        clothesListTitle.setText(getCategoryName(currentCategory) + "列表");
        
        // 更新适配器
        clothesAdapter.notifyDataSetChanged();
        
        // 更新空状态
        emptyClothesHint.setVisibility(filteredClothes.isEmpty() ? View.VISIBLE : View.GONE);
        clothesList.setVisibility(filteredClothes.isEmpty() ? View.GONE : View.VISIBLE);
    }
    
    private String getCategoryName(int categoryId) {
        for (Category category : categories) {
            if (category.getId() == categoryId) {
                return category.getName();
            }
        }
        return "";
    }
    
    private void toggleCategory(int categoryId) {
        if (currentCategory == categoryId) {
            // 点击当前分类，切换显示状态
            showClothesList = !showClothesList;
        } else {
            // 点击其他分类，切换分类并显示列表
            currentCategory = categoryId;
            showClothesList = true;
            loadClothesByCategory(categoryId);
        }
        
        // 更新UI
        clothesListOverlay.setVisibility(showClothesList ? View.VISIBLE : View.GONE);
        categoryAdapter.notifyDataSetChanged();
    }
    
    private void closeClothesList() {
        showClothesList = false;
        clothesListOverlay.setVisibility(View.GONE);
    }
    
    private void addToBoard(Clothes clothes) {
        if (!LoginCheckUtil.checkLoginAndRedirect(this, true)) {
            return;
        }
        
        // 检查是否已经添加过
        for (Clothes item : boardItems) {
            if (item.getId() == clothes.getId()) {
                Toast.makeText(this, "该衣物已添加到搭配中", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        
        // 克隆一个新对象，避免修改原数据
        Clothes newItem = new Clothes();
        newItem.setId(clothes.getId());
        newItem.setName(clothes.getName());
        newItem.setCategory(clothes.getCategory());
        newItem.setSeason(clothes.getSeason());
        newItem.setImageUrl(clothes.getImageUrl());
        
        // 设置初始位置
        newItem.setX(0);
        newItem.setY(0);
        newItem.setScale(1.0f);
        
        // 添加到列表
        boardItems.add(newItem);
        
        // 更新UI
        updateBoardUI();
        
        // 显示提示
        Toast.makeText(this, "已添加到搭配展示", Toast.LENGTH_SHORT).show();
    }
    
    private void removeFromBoard(int position) {
        if (position >= 0 && position < boardItems.size()) {
            boardItems.remove(position);
            updateBoardUI();
        }
    }
    
    private void updateBoardUI() {
        // 清空现有视图
        boardContent.removeAllViews();
        
        // 添加新视图
        for (int i = 0; i < boardItems.size(); i++) {
            addMovableClothesView(boardItems.get(i), i);
        }
        
        // 更新空状态提示
        emptyHint.setVisibility(boardItems.isEmpty() ? View.VISIBLE : View.GONE);
        
        // 更新调试信息
        debugInfo.setText("已加载图片数: " + boardItems.size());
    }
    
    private void addMovableClothesView(Clothes clothes, final int position) {
        // 创建视图
        View itemView = LayoutInflater.from(this).inflate(R.layout.item_movable_clothes, boardContent, false);
        ImageView imageView = itemView.findViewById(R.id.movableImage);
        TextView removeBtn = itemView.findViewById(R.id.removeBtn);
        TextView scalePlusBtn = itemView.findViewById(R.id.scalePlusBtn);
        TextView scaleMinusBtn = itemView.findViewById(R.id.scaleMinusBtn);
        
        // 加载图片
        if (!TextUtils.isEmpty(clothes.getImageUrl())) {
            Glide.with(this)
                .load(clothes.getImageUrl())
                .placeholder(R.drawable.placeholder_clothes)
                .error(R.drawable.placeholder_clothes)
                .into(imageView);
        } else {
            imageView.setImageResource(R.drawable.placeholder_clothes);
        }
        
        // 设置位置和缩放
        updateItemPosition(itemView, clothes);
        updateItemScale(itemView, clothes);
        
        // 设置移除按钮点击事件
        removeBtn.setOnClickListener(v -> removeFromBoard(position));
        
        // 设置缩放按钮点击事件
        scalePlusBtn.setOnClickListener(v -> {
            if (clothes.getScale() < 3.0f) {
                clothes.setScale(clothes.getScale() + 0.2f);
                updateItemScale(itemView, clothes);
            }
        });
        
        scaleMinusBtn.setOnClickListener(v -> {
            if (clothes.getScale() > 0.5f) {
                clothes.setScale(clothes.getScale() - 0.2f);
                updateItemScale(itemView, clothes);
            }
        });
        
        // 设置拖动事件
        itemView.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    isDragging = true;
                    currentIndex = position;
                    startX = event.getRawX() - clothes.getX();
                    startY = event.getRawY() - clothes.getY();
                    return true;
                    
                case MotionEvent.ACTION_MOVE:
                    if (isDragging && currentIndex == position) {
                        float newX = event.getRawX() - startX;
                        float newY = event.getRawY() - startY;
                        
                        // 限制范围
                        int maxX = boardContent.getWidth() - itemView.getWidth();
                        int maxY = boardContent.getHeight() - itemView.getHeight();
                        
                        newX = Math.max(0, Math.min(newX, maxX));
                        newY = Math.max(0, Math.min(newY, maxY));
                        
                        clothes.setX(newX);
                        clothes.setY(newY);
                        
                        updateItemPosition(itemView, clothes);
                    }
                    return true;
                    
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    isDragging = false;
                    currentIndex = -1;
                    return true;
            }
            return false;
        });
        
        // 添加到容器
        boardContent.addView(itemView);
    }
    
    private void updateItemPosition(View itemView, Clothes clothes) {
        itemView.setX(clothes.getX());
        itemView.setY(clothes.getY());
    }
    
    private void updateItemScale(View itemView, Clothes clothes) {
        itemView.setScaleX(clothes.getScale());
        itemView.setScaleY(clothes.getScale());
    }
    
    private void saveOutfit() {
        if (!LoginCheckUtil.checkLoginAndRedirect(this, true)) {
            return;
        }
        
        if (boardItems.isEmpty()) {
            Toast.makeText(this, "请先添加衣物", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 显示保存弹窗
        saveModal.setVisibility(View.VISIBLE);
        
        // 设置标题
        modalTitle.setText(isEditMode ? "编辑搭配" : "保存搭配");
        
        // 设置按钮文本
        btnModalConfirm.setText(isEditMode ? "确认修改" : "保存");
        
        // 更新预览列表
        previewAdapter.notifyDataSetChanged();
    }
    
    private void closeSaveModal() {
        saveModal.setVisibility(View.GONE);
        etOutfitName.setText("");
    }
    
    private void confirmSave() {
        String outfitName = etOutfitName.getText().toString().trim();
        
        if (TextUtils.isEmpty(outfitName)) {
            Toast.makeText(this, "请输入搭配名称", Toast.LENGTH_SHORT).show();
            return;
        }
        
        if (boardItems.isEmpty()) {
            Toast.makeText(this, "请选择衣物", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 显示保存中提示
        Toast.makeText(this, "保存中...", Toast.LENGTH_SHORT).show();
        
        // TODO: 实际应用中应该调用API保存数据
        
        // 模拟保存成功
        new android.os.Handler().postDelayed(() -> {
            Toast.makeText(MatchActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
            
            // 关闭弹窗
            closeSaveModal();
            
            // 重置状态
            resetOutfit();
            
            // 如果是编辑模式，返回上一页
            if (isEditMode) {
                finish();
            }
        }, 1000);
    }
    
    private void cancelEdit() {
        // 显示确认对话框
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        builder.setTitle("提示")
               .setMessage("确定要取消编辑吗？未保存的修改将会丢失")
               .setPositiveButton("确定", (dialog, which) -> {
                   resetOutfit();
                   finish();
               })
               .setNegativeButton("取消", null)
               .show();
    }
    
    private void resetOutfit() {
        boardItems.clear();
        updateBoardUI();
        currentCategory = 0;
        currentSeason = "all";
        showClothesList = false;
        clothesListOverlay.setVisibility(View.GONE);
        loadClothesByCategory(0);
    }
    
    private void showOutfitList() {
        if (!LoginCheckUtil.checkLoginAndRedirect(this, true)) {
            return;
        }
        
        // TODO: 跳转到搭配列表页面
        Toast.makeText(this, "跳转到搭配列表", Toast.LENGTH_SHORT).show();
    }
    
    private void loadOutfitForEdit() {
        // TODO: 实际应用中应该从API加载数据
        
        // 模拟加载数据
        Toast.makeText(this, "加载搭配数据...", Toast.LENGTH_SHORT).show();
        
        // 模拟数据
        new android.os.Handler().postDelayed(() -> {
            // 清空现有数据
            boardItems.clear();
            
            // 添加模拟数据
            for (int i = 1; i <= 3; i++) {
                Clothes clothes = new Clothes();
                clothes.setId(i);
                clothes.setName("搭配衣物" + i);
                clothes.setCategory(i % 2 == 0 ? "上衣" : "下装");
                clothes.setSeason("spring");
                clothes.setImageUrl("https://via.placeholder.com/150?text=Outfit" + i);
                clothes.setX(50 * i);
                clothes.setY(50 * i);
                clothes.setScale(1.0f);
                
                boardItems.add(clothes);
            }
            
            // 更新UI
            updateBoardUI();
            
            // 设置搭配名称
            etOutfitName.setText("我的搭配" + editOutfitId);
            
            Toast.makeText(this, "加载完成", Toast.LENGTH_SHORT).show();
        }, 1000);
    }
    
    private class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.CategoryViewHolder> {
        
        @NonNull
        @Override
        public CategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(MatchActivity.this).inflate(R.layout.item_match_category, parent, false);
            return new CategoryViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull CategoryViewHolder holder, int position) {
            Category category = categories.get(position);
            holder.tvCategoryName.setText(category.getName());
            
            // 设置选中状态
            boolean isSelected = category.getId() == currentCategory;
            holder.tvCategoryArrow.setText(isSelected ? "▼" : "▶");
            holder.itemView.setSelected(isSelected);
            
            holder.itemView.setOnClickListener(v -> {
                toggleCategory(category.getId());
            });
        }
        
        @Override
        public int getItemCount() {
            return categories.size();
        }
        
        class CategoryViewHolder extends RecyclerView.ViewHolder {
            TextView tvCategoryName;
            TextView tvCategoryArrow;
            
            CategoryViewHolder(@NonNull View itemView) {
                super(itemView);
                tvCategoryName = itemView.findViewById(R.id.tvCategoryName);
                tvCategoryArrow = itemView.findViewById(R.id.tvCategoryArrow);
            }
        }
    }
    
    private class ClothesAdapter extends RecyclerView.Adapter<ClothesAdapter.ClothesViewHolder> {
        
        @NonNull
        @Override
        public ClothesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(MatchActivity.this).inflate(R.layout.item_match_clothes, parent, false);
            return new ClothesViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull ClothesViewHolder holder, int position) {
            Clothes clothes = filteredClothes.get(position);
            
            holder.tvClothesName.setText(clothes.getName());
            holder.tvClothesTag.setText(clothes.getCategory());
            
            // 加载图片
            if (!TextUtils.isEmpty(clothes.getImageUrl())) {
                Glide.with(MatchActivity.this)
                    .load(clothes.getImageUrl())
                    .placeholder(R.drawable.placeholder_clothes)
                    .error(R.drawable.placeholder_clothes)
                    .into(holder.ivClothes);
            } else {
                holder.ivClothes.setImageResource(R.drawable.placeholder_clothes);
            }
            
            // 点击事件
            holder.itemView.setOnClickListener(v -> {
                // 选择衣物
            });
            
            // 长按事件
            holder.itemView.setOnLongClickListener(v -> {
                addToBoard(clothes);
                return true;
            });
        }
        
        @Override
        public int getItemCount() {
            return filteredClothes.size();
        }
        
        class ClothesViewHolder extends RecyclerView.ViewHolder {
            ImageView ivClothes;
            TextView tvClothesName;
            TextView tvClothesTag;
            
            ClothesViewHolder(@NonNull View itemView) {
                super(itemView);
                ivClothes = itemView.findViewById(R.id.ivClothes);
                tvClothesName = itemView.findViewById(R.id.tvClothesName);
                tvClothesTag = itemView.findViewById(R.id.tvClothesTag);
            }
        }
    }
    
    private class PreviewAdapter extends RecyclerView.Adapter<PreviewAdapter.PreviewViewHolder> {
        
        @NonNull
        @Override
        public PreviewViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(MatchActivity.this).inflate(R.layout.item_match_preview, parent, false);
            return new PreviewViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull PreviewViewHolder holder, int position) {
            Clothes clothes = boardItems.get(position);
            
            holder.tvPreviewName.setText(clothes.getName());
            
            // 加载图片
            if (!TextUtils.isEmpty(clothes.getImageUrl())) {
                Glide.with(MatchActivity.this)
                    .load(clothes.getImageUrl())
                    .placeholder(R.drawable.placeholder_clothes)
                    .error(R.drawable.placeholder_clothes)
                    .into(holder.ivPreview);
            } else {
                holder.ivPreview.setImageResource(R.drawable.placeholder_clothes);
            }
        }
        
        @Override
        public int getItemCount() {
            return boardItems.size();
        }
        
        class PreviewViewHolder extends RecyclerView.ViewHolder {
            ImageView ivPreview;
            TextView tvPreviewName;
            
            PreviewViewHolder(@NonNull View itemView) {
                super(itemView);
                ivPreview = itemView.findViewById(R.id.ivPreview);
                tvPreviewName = itemView.findViewById(R.id.tvPreviewName);
            }
        }
    }
} 